package com.ffyc.cemp.web.controller;


import com.ffyc.cemp.common.dto.HttpResult;
import com.ffyc.cemp.domain.entity.Movie;
import com.ffyc.cemp.domain.vo.PageVo;
import com.ffyc.cemp.services.service.IMovieService;
import com.ffyc.cemp.tools.Base64UtilX;
import com.ffyc.cemp.web.annotation.Permit;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.List;

@Api(tags = "电影api接口")
@RestController
@RequestMapping("/api/movie")
public class MovieController {

    @Autowired
    private IMovieService ims;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;


    @Permit(role = "2")
    @ApiOperation(value = "findAllMovies", notes = "查询所有的电影信息按照分页显示")
    @GetMapping("/findAllMovies")
    public HttpResult<PageVo<List<Movie>>> findAllMovies(int curr, int pageSize) {
        PageVo<List<Movie>> pageVo = ims.findAll(curr,pageSize);
        return HttpResult.success(pageVo);
    }

    @PostMapping("/posterUpload")
    @SneakyThrows
    public HttpResult<String> posterUpload(@RequestParam("file") MultipartFile file) {

        InputStream in = file.getInputStream();
        String type = file.getContentType();//"image/jpeg";
        type = type.substring(6);
        String imgStr = Base64UtilX.INSTANCE.encode(in, type);

        // stringRedisTemplate.opsForValue().set(imgStr.substring(0,100),imgStr);


        return HttpResult.success(imgStr);
    }

    @ApiOperation(value = "getMovieTypes", notes = "查询所有的电影类型")
    @GetMapping("/getMovieTypes")
    public HttpResult getMovieTypes() {
        return HttpResult.success(ims.findAllTypes());
    }

    @PostMapping("/addMoive")
    public HttpResult addMovie(@RequestBody Movie movie) {
        //System.err.println(movie);
        ims.addMovie(movie);
        return HttpResult.success("添加成功");
    }

    @ApiOperation(value = "loadWords", notes = "进行分词预处理")
    @GetMapping("/loadWords")
    public HttpResult loadWords() {
        ims.wordFromDb();
        return HttpResult.success("分词完成");
    }

    @ApiOperation(value = "findMoviesByKeywords", notes = "根据关键字进行电影查询")
    @GetMapping("/findMoviesByKeywords")
    public HttpResult<List<Movie>> findMoviesByKeywords(String keywords){
        return HttpResult.success(ims.findByKeywords(keywords));
    }

}
